<%-- 
    Document   : line
    Created on : 2013-5-7, 11:44:21
    Author     : piaohao
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%= basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="../main/common.jsp" %>
        <title>线路档案</title>
        <script type="text/javascript">
            Ext.onReady(function() {
                var lineForm = Ext.create("Ext.form.Panel", {
                    url: "line/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 3
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "始发站", name: "line.start_station"},
                        {fieldLabel: "终点站", name: "line.end_station"},
                        {fieldLabel: "线路名称", name: "line.name"},
                        {fieldLabel: "里程（公里）", name: "line.mileage", regex: Validator.number, regexText: "里程格式不正确"},
                        {fieldLabel: "线路开辟日期", name: "line_start_date", xtype: "datefield", editable: false, format: "Y-m-d"},
                        {
                            xtype: "combo",
                            fieldLabel: '线路类别',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "省际", value: "省际"},
                                    {text: "市际", value: "市际"},
                                    {text: "县际", value: "县际"},
                                    {text: "县内", value: "县内"},
                                    {text: "公交", value: "公交"},
                                    {text: "旅游", value: "旅游"},
                                    {text: "超长线", value: "超长线"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "line.type",
                            emptyText: "--请选择--",
                            value: ""
                        },
                        {
                            xtype: "combo",
                            fieldLabel: '线路等级',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "一类", value: "一类"},
                                    {text: "二类", value: "二类"},
                                    {text: "三类", value: "三类"},
                                    {text: "四类", value: "四类"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "line.level_",
                            emptyText: "--请选择--",
                            value: ""
                        },
                        {
                            id: "orgCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属机构",
                            name: "line.org_id",
                            displayField: 'name',
                            valueField: 'id',
                            editable: false,
                            colspan: 2,
                            emptyText: "--请选择--",
                            store: {
                                fields: ['name', 'id'],
                                proxy: {
                                    type: 'ajax',
                                    url: 'org/getAll',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            }
                        },
                        {fieldLabel: "途经站", name: "line.passed_station", colspan: 3, width: 700},
                        {fieldLabel: "备注", name: "line.remark", colspan: 3, width: 700},
                        {name: "line.id", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                lineStore.load();
                                                lineWindow.close();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                lineWindow.close();
                            }
                        }]
                });
                var lineWindow = Ext.create("Ext.window.Window", {
                    title: "线路档案",
                    width: 800,
                    height: 400,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "fit",
                    modal: true,
                    items: [lineForm]
                });
                var lineStore = Ext.create('Ext.data.Store', {
                    fields: ["id", "start_station", "end_station", "name", "mileage", "line_start_date", "type", "level_", "passed_station", "remark", "org_id"],
                    pageSize: 15,
                    proxy: {
                        type: 'ajax',
                        url: 'line/list',
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    },
                    autoLoad: true
                });
                var lineGrid = Ext.create("Ext.grid.Panel", {
                    title: "线路档案",
                    columnLines: true,
                    frame: true,
                    selType: 'checkboxmodel', // 设置选择模式 
                    multiSelect: true, // 允许多选
                    forceFit: true,
                    columns: [
                        {header: '主键', dataIndex: 'id', hidden: true},
                        {header: '所属机构', dataIndex: 'org_id', hidden: true},
                        {header: '始发站', dataIndex: 'start_station', width: 120},
                        {header: '终点站', dataIndex: 'end_station', width: 100},
                        {header: '线路名称', dataIndex: 'name', width: 200},
                        {header: '里程（公里）', dataIndex: 'mileage', width: 100},
                        {header: '线路开辟日期', dataIndex: 'line_start_date', width: 200},
                        {header: '线路类别', dataIndex: 'type', width: 100},
                        {header: '线路等级', dataIndex: 'level_', width: 100},
                        {header: '途经站', dataIndex: 'passed_station', width: 100},
                        {header: '备注', dataIndex: 'remark', width: 100}
                    ],
                    store: lineStore,
                    tbar: [
                        {xtype: "button", text: "查看", iconCls: "search",
                            handler: function() {
                                var sel = lineGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0].data;
                                var detailWindow = Ext.create("Ext.window.Window", {
                                    title: "线路档案",
                                    width: 800,
                                    height: 400,
                                    maximizable: true,
                                    modal: true,
                                    closeAction: "hide",
                                    layout: "fit",
                                    html: "<iframe id='detailFrame' src='line/showDetail/" + record.id + "' width='100%' height='100%'></iframe>",
                                    buttons: [
                                        {text: "打印", iconCls: "print",
                                            handler: function() {
                                                Ext.get("detailFrame").dom.contentWindow.print();
                                            }},
                                        {text: "关闭", iconCls: "close",
                                            handler: function() {
                                                detailWindow.close();
                                            }}
                                    ]
                                });
                                detailWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "登记", iconCls: "add",
                            handler: function() {
                                lineForm.getForm().reset();
                                lineForm.getForm().url = "line/add";
                                lineWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "变更", iconCls: "edit",
                            handler: function() {
                                var sel = lineGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0];
                                lineForm.getForm().url = "line/update";
                                lineWindow.show();
                                lineForm.getForm().setValues({
                                    "line.id": record.get("id"),
                                    "line.org_id": record.get("org_id"),
                                    "line.start_station": record.get("start_station"),
                                    "line.end_station": record.get("end_station"),
                                    "line.name": record.get("name"),
                                    "line.mileage": record.get("mileage"),
                                    "line_start_date": record.get("line_start_date").substring(0, 10),
                                    "line.type": record.get("type"),
                                    "line.level_": record.get("level_"),
                                    "line.passed_station": record.get("passed_station"),
                                    "line.remark": record.get("remark"),
                                });
                                Ext.getCmp("orgCombo").getStore().load();
                            }
                        },
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "删除", iconCls: "remove",
                            handler: function() {
                                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                    if (id === "yes") {
                                        var mask = new Ext.LoadMask(lineGrid, {msg: "正在删除..."});
                                        mask.show();
                                        var sel = lineGrid.getSelectionModel().getSelection();
                                        var ids = [];
                                        for (i = 0; i < sel.length; i++) {
                                            ids.push(sel[i].data.id);
                                        }
                                        Ext.Ajax.request({
                                            url: "line/delete",
                                            params: {
                                                ids: ids
                                            },
                                            success: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    lineGrid.getStore().remove(sel);
                                                    mask.hide();
                                                });
                                            },
                                            failure: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    mask.hide();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: lineStore,
                        displayInfo: true
                    })
                });
                var viewport = Ext.create("Ext.Viewport", {
                    layout: {type: "border"},
                    items: [
                        {region: "center", layout: "fit", items: [lineGrid]}
                    ]
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>
